package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vehicle-manager/cache"
	"vehicle-manager/models"
)

func CarAdd(c *gin.Context) {
	carNo := c.PostForm("car_no")
	carName := c.PostForm("car_name")
	carPrice, _ := strconv.Atoi(c.PostForm("car_price"))

	if carNo == "" || carName == "" || carPrice == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.Car{
		CarNo:    carNo,
		CarName:  carName,
		CarPrice: carPrice,
	}

	models.DB.Create(&data).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func ContractAdd(c *gin.Context) {
	contractNo := c.PostForm("contract_no")
	contractPrice := c.PostForm("contract_price")
	conName := c.PostForm("con_name")
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	if contractNo == "" || contractPrice == "" || conName == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.Contract{
		ContractNo:    contractNo,
		ContractPrice: contractPrice,
		ConName:       conName,
		Cid:           cid,
	}

	models.DB.Create(&data).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func CarContractList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size

	/*
		SELECT
		contracts.id AS ContractId,
		cars.car_no AS CarNo,
		cars.car_name AS CarName,
		cars.car_price AS CarPrice,
		contracts.contract_no AS ContractNo,
		contracts.contract_price AS ContractPrice,
		contracts.con_name AS ConName
		FROM
		contracts
		LEFT JOIN cars ON cars.id =contracts.cid
		WHERE contracts.id =1
	*/

	var contract []models.CarContract

	redis_key := "con_tract_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("contracts").
			Joins("LEFT JOIN cars ON cars.id =contracts.cid").
			Select(
				"contracts.id AS ContractId",
				"cars.car_no AS CarNo",
				"cars.car_name AS CarName",
				"cars.car_price AS CarPrice",
				"contracts.contract_no AS ContractNo",
				"contracts.contract_price AS ContractPrice",
				"contracts.con_name AS ConName",
			).
			Offset(offset).
			Limit(size).
			Debug().
			Find(&contract)

		jsonData, _ := json.Marshal(contract)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &contract)
	}

	var count int64
	models.DB.Model(&models.Contract{}).Count(&count)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count":    count,
			"contract": contract,
		},
	})
	return
}

func CarContractDetail(c *gin.Context) {
	id := c.Query("id")

	var contract []models.CarContract

	redis_key := "con_tract_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("contracts").
			Joins("LEFT JOIN cars ON cars.id =contracts.cid").
			Select(
				"contracts.id AS ContractId",
				"cars.car_no AS CarNo",
				"cars.car_name AS CarName",
				"cars.car_price AS CarPrice",
				"contracts.contract_no AS ContractNo",
				"contracts.contract_price AS ContractPrice",
				"contracts.con_name AS ConName",
			).
			Where("contracts.id =?", id).
			Debug().
			Find(&contract)

		jsonData, _ := json.Marshal(contract)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &contract)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": contract,
	})
	return
}
